import { webContents } from 'electron';
import { HTTP_BEGIN, HTTP_END } from '@/bglib/http_server';
import _ from "lodash";
import { Component, Vue, Prop } from "vue-property-decorator";
import { DEVTOOL_TOGGLE } from '@/bglib/electron_helper';


@Component
export default class HttpServer extends Vue {

    public is_http_running = false;
    public running_msg = "运行信息";
    public post = 8081;

    async created() {
        //await this.$ipcR.send(HTTP_END);
    }

    /**
     * 启动网站服务
     */
    async start() {
        const res = await this.$ipcR.send(HTTP_BEGIN, { post: this.post });
        this.is_http_running = true;
        this.running_msg = res.message;

        await this.addCss("outcss_01", "out01.css");
    }

    /** 添加样式 */
    async addCss(id: string, path: string) {
        let cssEl = document.getElementById(id) as HTMLLinkElement;
        if (cssEl) {
            cssEl.href = `http://localhost:${this.post}/res/css/${path}`;
        }
        else {
            let head = document.querySelector("head")
            if (head) {
                let link = document.createElement("link");
                link.id = id;
                link.rel = "stylesheet";
                link.type = "text/css";
                link.href = `http://localhost:${this.post}/res/css/${path}`;
                head.appendChild(link);
            }
        }
    }

    async stop() {
        await this.$ipcR.send(HTTP_END);
        this.is_http_running = false;
    }



    async devTool() {
        await this.$ipcR.send(DEVTOOL_TOGGLE);
    }

}